package com.gecba.system.service.impl;

import com.gecba.minio.enums.ContentType;
import com.gecba.minio.service.MinIoServices;
import com.gecba.modular.jpa.service.impl.ServiceImpl;
import com.gecba.system.dao.repository.SysOssFileRepository;
import com.gecba.system.entity.OssFileState;
import com.gecba.system.entity.OssFileType;
import com.gecba.system.entity.SysOssFile;
import com.gecba.system.service.SysOssFileService;
import io.minio.ObjectWriteResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Service
@Slf4j
public class SysOssFileServiceImpl extends ServiceImpl<SysOssFileRepository, SysOssFile,Long> implements SysOssFileService {


    @Autowired
    private MinIoServices minIoServices;

    /**
     * @param fileName
     * @return String
     * @description 获取文件后缀名称后缀名
     */
    private String getFileSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }


    private String getOssFileName(String fileName){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return uuid + getFileSuffix(fileName);
    }

    private ContentType getContentType(String fileName){
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        switch (suffix){
            case "jpg":
            case "jpeg":
                return ContentType.JPEG;
            case "gif":
                return ContentType.GIF;
            case "png":
                return ContentType.PNG;

        }
        return ContentType.MULTIPART;
    }




    /**
     * OSS上传文件
     *
     * @param multipartFile
     * @return
     */
    @Override
    @SneakyThrows(Exception.class)
    public SysOssFile upload(MultipartFile multipartFile) {
        SysOssFile ossFile = new SysOssFile();
        String sourcesFileName = multipartFile.getOriginalFilename();
        String ossFileName = getOssFileName(sourcesFileName);
        long size = multipartFile.getSize();
        InputStream inputStream = multipartFile.getInputStream();
        ContentType contentType = getContentType(ossFileName);
        ObjectWriteResponse image = minIoServices.putObject("image", ossFileName, inputStream, contentType);

        ossFile.setSourceFileName(sourcesFileName);
        ossFile.setFileName(image.object());
        ossFile.setContentType(contentType);
        ossFile.setFileSize(size);
        ossFile.setFileUri(image.bucket()+"/"+image.object());
        ossFile.setFileUrl("http://139.198.26.118:9000/"+ossFile.getFileUri());
        ossFile.setFileType(OssFileType.IMG);
        ossFile.setFileState(OssFileState.TEMP);
        return baseRepository.save(ossFile);
    }

    /**
     * OSS删除文件
     *
     * @param fileName
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByFileName(String fileName) {
        try{
            Integer integer = baseRepository.deleteByFileName(fileName);
            if(integer>0){
                minIoServices.deleteObject("image",fileName);
                return 1;
            }
            return 0;
        }catch (Exception e){
            log.error("删除图片异常>>>",e);
            return 0;
        }
    }
}
